package com.wugenqiang.project.inventory.controller;

import com.wugenqiang.constant.ParamConstant;
import com.wugenqiang.project.inventory.pojo.Storage;
import com.wugenqiang.project.inventory.pojo.Warehouse;
import com.wugenqiang.project.inventory.pojo.vo.WarehouseVo;
import com.wugenqiang.project.inventory.service.IStorageService;
import com.wugenqiang.project.inventory.service.IWarehouseService;
import com.wugenqiang.project.system.controller.BaseController;
import com.wugenqiang.utils.date.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname WarehouseController
 * @Description TODO
 * @Date 2019/3/8 16:21
 * @Created by WuGenQiang//fighting!!
 */
@Controller
@RequestMapping("/lfd/admin/inventory/warehouse")
public class WarehouseController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(WarehouseController.class);

    @Resource
    private IStorageService storageService;

    @Resource
    private IWarehouseService warehouseService;

    @RequestMapping(value = "")
    public String ShowWarehouseGoodsList(Model model) {

        List<WarehouseVo> warehouseVos = new ArrayList<>();
        List<Warehouse> warehouses = null;
        warehouses = warehouseService.queryAllGoods();
        LOGGER.info("start------"+warehouses.size());
        for(int i=0;i<warehouses.size();i++){
            WarehouseVo warehouseVo = new WarehouseVo();
            warehouseVo.setId(warehouses.get(i).getId());
            warehouseVo.setIdentifier(warehouses.get(i).getIdentifier());
            Storage storage = storageService.queryGoodsByIdentifier(warehouses.get(i).getIdentifier());
            warehouseVo.setName(storage.getName());
            warehouseVo.setModel(storage.getModel());
            warehouseVo.setAmount(warehouses.get(i).getAmount());
            warehouseVo.setUnit(storage.getUnit());
            warehouseVo.setAcceptor(warehouses.get(i).getAcceptor());
            warehouseVo.setTime(DateUtils.formatDateByUnixTime(warehouses.get(i).getTime(),"yyyy-MM-dd"));
            warehouseVo.setRemark(warehouses.get(i).getRemark());
            warehouseVos.add(warehouseVo);
        }
        LOGGER.info(warehouseVos.toString());
        model.addAttribute("warehouseVos", warehouseVos);
        return "inventory/warehouse_list";
    }

    // 跳转到入库页面
    @RequestMapping(value = "/toAddWarehouse")
    public String toAddWarehouse(HttpServletRequest request, Model model) {
        Integer id = Integer.parseInt(request.getParameter("id").trim());
        LOGGER.info(id+"");
        Storage storage = storageService.queryGoodsById(id);
        WarehouseVo warehouseVo = new WarehouseVo();
        warehouseVo.setId(id);
        warehouseVo.setIdentifier(storage.getIdentifier());
        warehouseVo.setName(storage.getName());
        warehouseVo.setAmount(0);
        warehouseVo.setModel(storage.getModel());
        warehouseVo.setUnit(storage.getUnit());
        warehouseVo.setRemark("");
        warehouseVo.setAcceptor("");
        warehouseVo.setTime("");
        LOGGER.info(warehouseVo.toString());
        model.addAttribute("warehouseVo", warehouseVo);
        return "inventory/warehouse_add";
    }

    // 更新操作
    @RequestMapping(value = "/addWarehouse", method = RequestMethod.POST)
    public String addWarehouse(Model model, HttpServletRequest request) {

        Integer id = Integer.parseInt(request.getParameter("id").trim());
        Storage storage = storageService.queryGoodsById(id);
        Warehouse warehouse = new Warehouse();
        warehouse.setIdentifier(storage.getIdentifier());
        String time = request.getParameter("time");
        if(time==null||time==""){
            warehouse.setTime(DateUtils.getCurrentUnixTime());
        }else{
            warehouse.setTime((int) (DateUtils.dateFormat(time, "yyyy-MM-dd").getTime() / 1000L));
        }
        warehouse.setAmount(Integer.valueOf(request.getParameter("amount")));
        warehouse.setRemark(request.getParameter("remark"));
        warehouse.setAcceptor(request.getParameter("acceptor"));
        if(storage.getAmount()!=0){
            storage.setAmount(warehouse.getAmount()+storage.getAmount());
        }else{
            storage.setBalance(warehouse.getAmount()+storage.getBalance());
        }
        storageService.updateGoodsInfo(storage);
        String result = warehouseService.addGoodsInfo(warehouse);
        if (result.equals(ParamConstant.INSERT_SUCCESS) == true) {

            List<Storage> storages = null;
            storages = storageService.queryAllGoods();
            model.addAttribute("storages", storages);
            return "inventory/storage_list";
        } else {
            return "inventory/warehouse_add";

        }
    }
}
